<template>
  <div class="login_container">
    <div class="login_box">
      <!--这是logo图片 -->
      <div class="img_box">
        <img src="../assets/img/logo.jpg" alt="图片丢失" />
      </div>
      <!--这是表单元素 -->
      <el-form
        label-width="0px"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        class="login_form"
      >
        <el-form-item prop="phone">
          <el-input placeholder="请输入手机号" v-model="loginForm.phone" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            show-password
            prefix-icon="el-icon-goods"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="restLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        phone: "",
        password: ""
      },
      loginFormRules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "手机号格式有误", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码长度小于6位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    restLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return;
        } else {
          const { data: result } = await this.$http.get("admin/login", {
            params: {
              phone: this.loginForm.phone,
              password: this.loginForm.password
            }
          });
          if (result.code != 200) {
            this.$message({
              message: "账号或者密码错误",
              type: "error"
            });
            return;
          } else {
            this.$message({
              message: "登录成功！",
              type: "success"
            });
            window.sessionStorage.setItem("token",result.data);
            this.$router.push("/home");
          }
        }
      });
    }
  }
};
</script>
<style lang="less" scope>
.login_container {
  background-image: url("../assets/img/background.jpg");
  height: 100%;
}
.login_box {
  width: 50%;
  height: 50%;
  background-color: whitesmoke;
  border-radius: 30px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.img_box {
  width: 200px;
  height: 200px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 11px;
  box-shadow: 0 0 11px gray;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: whitesmoke;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  bottom: 0px;
  position: absolute;
  width: 50%;
  padding: 24px;
  box-sizing: border-box;
  left: 50%;
  transform: translate(-50%);
}
</style>
